﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using Core.BusinessLogic.Interfaces;
using Core.Interfaces;
using Core.Common;
using Core.DataAccess;
using SonCa.SocialNetwork.Web.Friends.IPresenters;
using SonCa.SocialNetwork.Web.HelperClass;

namespace SonCa.SocialNetwork.Web.Friends.Presenters
{
    public class FriendsDisplayPresenter
    {
        private IFriendsDisplayPresenter _view;
        [Import]
        private IUserSession _userSession;
        [Import]
        private IAccountService _accountService;
        [Import]
        private IFriendService _friendService;
        [Import]
        private IFriendInvitationService _friendInvitationService;
        [Import]
        private IStatusUpdateService _statusUpdateService;
        [Import]
        private IRedirector _redirector;
        [Import]
        private IFileService _fileService;
        [Import]
        private IRatingService _ratingService;
        [Import]
        private IFolderService _folderService;
        [Import]
        private ILevelService _levelService;

        private Core.DataAccess.Account _accountBeingViewed;

        public FriendsDisplayPresenter()
        {
            MEFManager.Compose(this);
        }

        public void Init(IFriendsDisplayPresenter View, string username)
        {
            _view = View;
            if (string.IsNullOrEmpty(username))
                _accountBeingViewed = _userSession.CurrentUser;
            else
                _accountBeingViewed = _accountService.GetAccountByUsername(username);

            if (_accountBeingViewed != null)
            {
                List<FriendsProfile> friendsList = _friendService.GetFriendsAccountByAccountID(_accountBeingViewed.AccountID);                                
                List<StatusUpdate> statusList = _statusUpdateService.GetNewestStatusUpdateOfFriends(friendsList.Select(n => n.AccountID).ToList());
                List<File> files = _fileService.GetFilesByFolderID(_folderService.GetFolderIdByAccountId(friendsList.Select(n => n.AccountID).ToList()));
                List<FriendsInvitation> friendsInvitation = _friendInvitationService.GetListFriendsRequestByAccountID(_accountBeingViewed.AccountID);
                List<FriendsProfile> lstResult = new List<FriendsProfile>();
                foreach (FriendsProfile friendPro in friendsList)
                {                    
                    List<StatusUpdate> stUpList = statusList.FindAll(n => n.AccountID == friendPro.AccountID);
                    foreach (StatusUpdate stUp in stUpList)
                    {
                        FriendsProfile frProResult = new FriendsProfile();
                        frProResult.AccountID = friendPro.AccountID;
                        frProResult.FullName = friendPro.FullName;
                        frProResult.IsFriend = friendPro.IsFriend;

                        frProResult.Status = stUp.Status;
                        frProResult.StatusID = stUp.StatusUpdateID;
                        frProResult.CreateDateStatus = stUp.CreateDate;

                        if (stUp.StatusType == 2 || stUp.StatusType == 3 || stUp.StatusType == 4)   // Comment with photo, video, audio
                        {
                            List<File> fiList = files.FindAll(n => n.AccountID == friendPro.AccountID);
                            foreach (File fi in fiList)
                            {
                                if (fi.IsPublicResource)    // true: da post ; false: chua post
                                {
                                    frProResult.FileID = fi.FileID.ToString();
                                    frProResult.FileName = fi.FileName;
                                    frProResult.FileSystemFolderID = fi.FileSystemFolderID;
                                    frProResult.FileSystemName = fi.FileSystemName.ToString();
                                    frProResult.Extension = frProResult.FileName.Substring(frProResult.FileName.LastIndexOf(".") + 1);
                                    frProResult.CreateDateStatus = fi.CreateDate;
                                    frProResult.CreateDatePhotos = fi.CreateDate;
                                    files.Remove(fi);
                                    break;
                                }
                            }
                        }
                        frProResult.StatusType = Convert.ToInt32(stUp.StatusType);
                        frProResult.LikeCount = _ratingService.TotalLikeNumber(frProResult.AccountID, frProResult.StatusID)[0];
                        frProResult.UnlikeCount = _ratingService.TotalLikeNumber(frProResult.AccountID, frProResult.StatusID)[1];
                        if(friendsInvitation != null && friendsInvitation.Count > 0)
                            if (!friendsInvitation.Select(n => n.AccountID).ToList().Contains(friendPro.AccountID) && 
                                friendsInvitation.Select(n => n.BecameAccountID).ToList().Contains(friendPro.AccountID) &&
                                friendPro.IsFriend == false)
                                frProResult.WaitingAccept = true;

                        lstResult.Add(frProResult);
                    }                                        
                }
                lstResult = lstResult.OrderByDescending(n => n.CreateDateStatus).ThenByDescending(n => n.CreateDateStatus.TimeOfDay).ToList();
                _view.LoadFriends(lstResult);
            }
            else
            {
                _redirector.GoToLoginPage();
            }
        }

        public int GetLevelIDByAccountID(int accountID)
        {
            return _accountService.GetLevelIDByAccountID(accountID);
        }

        public string GetLevelPathByLevelID(int levelID)
        {
            return _levelService.GetLevelPathByLevelID(levelID);
        }

        public int GetScoreByAccountID(int accountID)
        {
            return _accountService.GetScoreByAccountID(accountID);
        }
    }
}